package edu.ynu.e_platform.service;

import edu.ynu.e_platform.common.service.LogicService;
import edu.ynu.e_platform.dao.*;
import edu.ynu.e_platform.dto.ItemDTO;
import edu.ynu.e_platform.entities.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author manwen Yang
 */
@Slf4j
@Service
public class BusinessService extends LogicService<BusinessDao, Business, Long> {

    @Resource
    UserDao userDao;

    @Resource
    ItemDao itemDao;

    @Resource
    ShopDao shopDao;

    @Resource
    OrderDao orderDao;

    public BusinessService(@Autowired BusinessDao lr) {
        super(lr);
    }

    /**
     * 商家注册
     * */
    public Business enroll(String userName, String password, Integer sex){

        User tem = null;
        Business business = new Business();
        business.setUsername(userName);
        business.setPassword(password);
        business.setSex(sex);

        try {
            tem = userDao.findByUsername(userName);
            //如果该用户名已被注册，返回空，说明注册失败
            if(tem != null){
                return null;
            } else{
                business = getDAO().save(business);
                return business;
            }
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return business;
    }

    /**
     * 商家创建店铺
     * */
    public Shop CreateShop(Long businessId, String shopName){

        Shop newShop = null;
        try {
            Business business = getDAO().getReferenceById(businessId);
            newShop = business.createShop(shopName);
            newShop = shopDao.save(newShop);
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return newShop;
    }

    /**
     * 商家创建商品
     * */
    public Item CreateItem(Long businessId,
                           String itemName,
                           Integer type,
                           String description,
                           Double price){

        String result = "";
        Item item = new Item();
        Business business = getDAO().getReferenceById(businessId);

        item.setBusiness(business);
        item.setItemName(itemName);
        item.setItemType(type);
        item.setDescription(description);
        item.setPrice(price);

        try {

            item = itemDao.save(item);
            business.createItem(item);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return item;
    }

    /**
     * 查询所有门店
     * */
    public List<Shop> queryShops(Long userId){

        Business business = null;

        try {
            business = getDAO().getReferenceById(userId);

            return shopDao.findByBusiness(business);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    /**
     * 商家查询所有订单
     * */
    public List<Order> queryOrders(Long shopId){

        Shop shop = null;

        try {
            shop = shopDao.getReferenceById(shopId);

            return orderDao.findByShop(shop);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    /**
     * 商家确认订单
     * */
    public Order confirmOrder(Long userId, Long orderId){

        Business business = null;
        Order order = null;

        try {
            business = getDAO().getReferenceById(userId);
            order = orderDao.getReferenceById(orderId);

            order = business.affirmOrder(order);

            return orderDao.save(order);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    /**
     * 商家取消订单
     * */
    public Order cancelOrder(Long userId, Long orderId){

        Business business = null;
        Order order = null;

        try {
            business = getDAO().getReferenceById(userId);
            order = orderDao.getReferenceById(orderId);

            order = business.cancelOrder(order);

            return orderDao.save(order);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    public Page<Shop> queryShopsPage(Integer pageStart, Integer pageSize, Long userId){

        try {
            Business business = getDAO().getReferenceById(userId);

            Pageable pageable = PageRequest.of(pageStart, pageSize, Sort.by("createTime").descending());

            return shopDao.find(business, pageable);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    public List<Item> queryItems(Long BusinessId){

        Business business = new Business();

        try {
            business = getDAO().getReferenceById(BusinessId);

            return itemDao.findByBusiness(business);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    public Page<Item> pageQueryItems(Integer pageStart, Integer pageSize, Long businessId){

        Business business = new Business();
        try {
            business = getDAO().getReferenceById(businessId);

            Pageable pageable = PageRequest.of(pageStart, pageSize, Sort.by("createTime").descending());

            return itemDao.find(business, pageable);

        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }
}

